package org.sg20.searchengine.search;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.sg20.searchengine.sort.SortDriver;


import java.io.*;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


public class SearchDriver {
    static void Sort(String infile, String outfile) throws IOException, URISyntaxException, InterruptedException, ClassNotFoundException {
        File file = new File(infile);

        long fileSize = FileUtils.sizeOf(file);
        if(fileSize>20){// For a file that has big size, we should use mapreduce to sort
            String[] args = {infile, outfile};
            SortDriver.main(args);
            return;

        }else {
            // small size file. common sort
            BufferedReader in = new BufferedReader(new FileReader(infile));
            BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
            // read until end of file
            List<String> lines = new ArrayList<>();
            String line;
            while ((line = in.readLine()) != null) {
                // add the name to the trie tree
                lines.add(line);
            }
            in.close();
            Collections.sort(lines);
            Collections.reverse(lines);

            out.write("Search Result is below :\n");
            out.write("OccurTimes\tFile\tLineoff\n");
            for (String str : lines) {
                out.write(str + "\n");
            }

            out.close();
        }
    }


    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException, URISyntaxException {

        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        String str = scanner.readLine();

        long startTime = System.currentTimeMillis();

        //set up configuration for mapreduce job
        Configuration conf = new Configuration();

        //Global information
        conf.setStrings("input", str);

        //Parse arguments
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Please input: xxx.jar InvertedIndexDriver [InputPath] [OutputPath].");
            System.exit(1);
        }

        Job job = Job.getInstance(conf, "org.sg20.searchengine.invertedindex.Search");

        job.setJarByClass(SearchDriver.class);
        job.setMapperClass(SearchMapper.class);
        job.setReducerClass(SearchReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileSystem fs = FileSystem.get(conf);

        String outpath = otherArgs[1] + "Search";
        if (fs.exists(new Path(outpath)))
            fs.delete(new Path(outpath), true);
        FileOutputFormat.setOutputPath(job, new Path(outpath));

        boolean status = job.waitForCompletion(true);

        Sort(outpath+"/part-r-00000", outpath+"/Sort");

        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间： " + (endTime - startTime) + "ms");

        System.exit(status ? 0 : 1);
    }
}
